using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
namespace Client.Hotfix
{
    [UGFM(UGFMWeight.UIService)]
    public class UIService : IAwake
    {
        private readonly Dictionary<Type, BaseUI> uiMap = new();
        public void Awake()
        {
            this.Log("UIService is Running");
        }

        public async UniTask<T> Show<T>() where T : BaseUI, new()
        {
            if (uiMap.TryGetValue(typeof(T), out BaseUI baseUI))
            {
                await baseUI.Show();
            }
            else
            {
                baseUI = new T();
                await baseUI.Show();
                uiMap.Add(typeof(T), baseUI);
            }

            return (T)baseUI;

        }

        public async void Show<T>(Action<T> callBack = null) where T : BaseUI, new()
        {
            if (uiMap.TryGetValue(typeof(T), out BaseUI baseUI))
            {
                await baseUI.Show();
                return;
            }
            else
            {
                baseUI = new T();
                await baseUI.Show();
                uiMap.Add(typeof(T), baseUI);

            }
            callBack?.Invoke((T)baseUI);

        }
        public void Hide<T>() where T : BaseUI
        {
            if (uiMap.TryGetValue(typeof(T), out BaseUI baseUI))
            {
                baseUI.Hide();
            }
        }


        public void Destroy<T>() where T : BaseUI
        {
            if (uiMap.TryGetValue(typeof(T), out BaseUI baseUI))
            {
                baseUI.Destroy();
                uiMap.Remove(typeof(T));
            }
        }
    }

}
